import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/utils/log.dart';
import 'package:kq_flutter_widgets/resources/kq_theme_colors.dart';

/// @author 高裕
/// 科学试模评价进度条
class ProgressForScienceTestMoldEvaluate extends StatefulWidget {
  const ProgressForScienceTestMoldEvaluate(
      {super.key,
      required this.gradientColor,
      required this.cutPart,
      required this.progress,
      this.space,
      this.topCustomView,
      this.bottomCustomView});

  /// 渐变色
  final List<Color> gradientColor;

  /// 切成几段
  final int cutPart;

  /// 进度 0 ~ 100
  final int progress;

  /// 间距
  final double? space;

  final Widget Function(double pointDx, double maxWidth)? topCustomView;

  final Widget Function(double pointDx, double maxWidth)? bottomCustomView;

  @override
  ProgressForScienceTestMoldEvaluateState createState() =>
      ProgressForScienceTestMoldEvaluateState();
}

class ProgressForScienceTestMoldEvaluateState
    extends State<ProgressForScienceTestMoldEvaluate> {
  // 渐变色 [[a,b][c,d]]
  List<List<Color>> colorList = [];

  /// 圆点在第几段,从1开始
  int? pointInPart;

  @override
  void initState() {
    super.initState();
    pointInPart = widget.progress ~/ (100 / widget.cutPart) +
        (widget.progress < 100 ? 1 : 0);
    double s = (widget.gradientColor.length - 1) / widget.cutPart;
    for (int i = 1; i <= widget.cutPart; i++) {
      try {
        if (i == widget.cutPart) {
          colorList.add([
            getGradientPercentColor(widget.gradientColor[(i - 1) * s ~/ 1],
                widget.gradientColor[((i - 1) * s ~/ 1) + 1], (i - 1) * s % 1),
            getGradientPercentColor(
                widget.gradientColor[widget.gradientColor.length - 2],
                widget.gradientColor[widget.gradientColor.length - 1],
                1),
          ]);
        } else {
          colorList.add([
            getGradientPercentColor(widget.gradientColor[(i - 1) * s ~/ 1],
                widget.gradientColor[((i - 1) * s ~/ 1) + 1], (i - 1) * s % 1),
            getGradientPercentColor(widget.gradientColor[i * s ~/ 1],
                widget.gradientColor[(i * s ~/ 1) + 1], i * s % 1),
          ]);
        }
      } catch (e) {
        Log.e(e);
      }
    }
  }

  @override
  void didUpdateWidget(covariant ProgressForScienceTestMoldEvaluate oldWidget) {
    super.didUpdateWidget(oldWidget);
    pointInPart = widget.progress ~/ (100 / widget.cutPart) +
        (widget.progress < 100 ? 1 : 0);
  }

  @override
  Widget build(BuildContext context) {
    final pointRadius = 6.r; // 必要时可提出
    return LayoutBuilder(builder: (context, constraints) {
      double pointX = ((constraints.maxWidth -
                  ((widget.space ?? 4.r) * (widget.cutPart - 1)) -
                  (pointRadius * widget.cutPart)) *
              widget.progress /
              100) +
          (((widget.space ?? 4.r) + pointRadius) * (pointInPart! - 1));
      return Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (widget.topCustomView != null)
            widget.topCustomView!.call(pointX, constraints.maxWidth),
          SizedBox(
            height: 6.r,
            child: Stack(
              children: [
                Row(
                  children: [
                    ...gradientProgressBars(),
                  ],
                ),
                Positioned(
                    top: 0,
                    left: pointX,
                    child: Container(
                      height: pointRadius,
                      width: pointRadius,
                      decoration: BoxDecoration(
                        color: KqThemeColors.bgWhite,
                        borderRadius:
                            BorderRadius.all(Radius.circular(pointRadius / 2)),
                      ),
                    )),
              ],
            ),
          ),
          if (widget.bottomCustomView != null)
            widget.bottomCustomView!.call(pointX, constraints.maxWidth),
        ],
      );
    });
  }

  List<Widget> gradientProgressBars() {
    List<Widget> listWidget = [];
    for (int i = 0; i < colorList.length; i++) {
      List<Color> color = colorList[i];
      if (color.length != 2) {
        throw UnimplementedError('colorList format error');
      }
      if (i != 0) {
        listWidget.add(SizedBox(
          width: widget.space ?? 4.r,
        ));
      }
      try {
        listWidget.add(Expanded(
            child: Container(
          height: 6.r,
          decoration: BoxDecoration(
            gradient: LinearGradient(
                //渐变位置
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                stops: const [0.0, 1.0], //[渐变起始点, 渐变结束点]
                colors: [color[0], color[1]]),
            borderRadius: BorderRadius.all(Radius.circular(3.r)),
          ),
        )));
      } catch (e) {
        Log.e(e);
      }
    }
    return listWidget;
  }

  /// 获取渐变色渐变过程中的颜色
  /// [percent] [0.0~1.0]
  static Color getGradientPercentColor(
      Color startColor, Color endColor, double percent) {
    int a = startColor.alpha +
        ((endColor.alpha - startColor.alpha) * percent).toInt();
    int r =
        startColor.red + ((endColor.red - startColor.red) * percent).toInt();
    int g = startColor.green +
        ((endColor.green - startColor.green) * percent).toInt();
    int b =
        startColor.blue + ((endColor.blue - startColor.blue) * percent).toInt();
    return Color.fromARGB(a, r, g, b);
  }
}
